package student.service;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import student.beans.Student;
import student.dao.StudentDAOMapper;

@Service
public class StudentService {
	@Autowired
	StudentDAOMapper studentMapper;
	/*@Autowired
	SqlSessionFactoryBean sessionfb;*/
	/*public StudentService(){
		ApplicationContext ioc= new ClassPathXmlApplicationContext("applicationContext.xml");
		studentMapper =ioc.getBean(StudentDAOMapper.class);
	}*/
	public Student createStudentBean(String item,String term) {
		Student student=new Student();
		if(item!=null) {
			switch(item) {
			case "Id":
				student.setStuId(Integer.parseInt(term));
				break;
			case "name":
				student.setStuName("%"+term+"%");;
				break;
			case "phone":
				student.setPhone("%"+term+"%");
				break;
			}				
		}
		return student;
	}
	public void addOneStudent(Student student){
		studentMapper.addStudent(student);
	}
	
	
	public PageInfo<Student> getPageInfo(int pageNo) {
		//获取第1页，2条内容，默认查询总数count
		PageHelper.startPage(pageNo, 2);
		List<Student> students=studentMapper.getAllStudent();
		//用PageInfo对结果进行包装
		return new PageInfo<Student>(students,5);		
	}
	//根据条件去查学生
	public PageInfo<Student> getPageInfoByItem(Integer pageNo,Student stu){
		PageHelper.startPage(pageNo, 2);
		List<Student> students=studentMapper.getStudentbyTerm(stu);
		//用PageInfo对结果进行包装
		return new PageInfo<Student>(students,5);
	}
	public void editStudent(Student student) {
		studentMapper.editStudent(student);
	}
	public void deleteStuById(Integer id) {
		studentMapper.deleteById(id);
	}
	public void deleteStuByIds(List<Integer> stuIds) {
		System.out.println(stuIds);
		studentMapper.deleteByIds(stuIds);
	}
}
